﻿using Newtonsoft.Json;
using System.Security.Cryptography;

namespace SageKing.Core.Extensions;

public static class HttpContentExts
{
    public static async Task<T> FromJSONAsync<T>(this HttpContent httpContent)
    {
        var content = await httpContent.ReadAsStringAsync();
        var result = content.JsonTo<T>();
        return result;
    }

    public static async Task<SageKingResult<T>> FromAdminResultAsync<T>(this HttpContent httpContent, bool checkSuccess = true)
    {
        var content = await httpContent.ReadAsStringAsync();
        var result = content.JsonTo<SageKingResult<T>>();
        if (checkSuccess && !result.IsSuccess)
        {
            throw new Exception(result.Message);
        }
        return result;
    }

}
